package com.shuoyue.ycgk.ui.shop;

import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;
import com.shuoyue.ycgk.entity.ShopCarBean;
import com.shuoyue.ycgk.utils.NumberUtils;
import com.shuoyue.ycgk.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车适配器
 */
public class ShoppingCartAdapter extends AppBaseQuickAdapter<ShopCarBean> {


    public ShoppingCartAdapter(List data) {
        super(R.layout.item_shopping_cart, data);
    }


    @Override
    protected void convert(BaseViewHolder helper, ShopCarBean item) {
        helper.addOnClickListener(R.id.tv_add);
        helper.addOnClickListener(R.id.tv_reduce);
        helper.addOnClickListener(R.id.lay_all);
        helper.addOnClickListener(R.id.tv_check);


        CheckBox tv_check = helper.getView(R.id.tv_check);
        TextView tv_num = helper.getView(R.id.tv_num);
        helper.setText(R.id.tv_title, StringUtil.stringEmpty(item.getProduct().getName()));
        helper.setText(R.id.tv_price, item.getProduct().getDefaultPrice() == null
                ? "免费"
                : ("￥" + item.getProduct().getDefaultPrice().getPrice()));
        helper.setText(R.id.tv_num, "" + item.getNum());

        tv_num.setText(String.valueOf(item.getNum()));
        tv_check.setChecked(item.isSelect());
        Glide.with(mContext)
                .load(item.getProduct().getCover())
                .error(R.mipmap.img_loading_gray)
                .placeholder(R.mipmap.img_loading_gray)
                .into((ImageView) helper.getView(R.id.iv_img));


    }

    public List<ShopCarBean> getSelect() {
        List<ShopCarBean> shopCarBeans = new ArrayList<>();
        for (ShopCarBean shopCarBean : mData) {
            if (shopCarBean.isSelect()) {
                shopCarBeans.add(shopCarBean);
            }
        }
        return shopCarBeans;
    }
}